﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ST.Buz.Base;
using ST.Buz.Service;
using ST.Common.Util.Mvc;
using System;
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace ST.Buz.WinService
{
    public class Startup
    {
        private readonly ILogger logger;
        public Startup(IConfiguration configuration, ILogger<Startup> _logger)
        {
            Configuration = configuration;
            logger = _logger;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                string connStrBuz = Configuration.GetConnectionString("BuzConnection");
                services.AddDbContext<BuzContext>(options => options.UseSqlServer(connStrBuz, opts =>
                {
                    //opts.UseRowNumberForPaging();//不再支持ROW_NUMBER()分页方式
                })
                //.AddInterceptors(new MyDbCommandInterceptor()
                );

                #region 添加静态类
                //services.AddSingleton(typeof(IAppManager), typeof(AppManager));
                //services.AddSingleton<AccessTokenManager>();
                //services.AddSingleton<BaseApi>();
                #endregion

                #region 添加业务service
                //services.AddScoped(typeof(IServiceDeviceConfig), typeof(ServiceDeviceConfig));
                #endregion

                #region 添加系统service
                services.AddScoped(typeof(IServiceSysArea), typeof(ServiceSysArea));
                services.AddScoped(typeof(IServiceSysAttachment), typeof(ServiceSysAttachment));
                services.AddScoped(typeof(IServiceSysAuditLog), typeof(ServiceSysAuditLog));
                services.AddScoped(typeof(IServiceSysDept), typeof(ServiceSysDept));
                //services.AddScoped(typeof(IServiceSysDuty), typeof(ServiceSysDuty));
                services.AddScoped(typeof(IServiceSysEnumCode), typeof(ServiceSysEnumCode));
                //services.AddScoped(typeof(IServiceSysExpress), typeof(ServiceSysExpress));
                //services.AddScoped(typeof(IServiceSysInvoice), typeof(ServiceSysInvoice));
                services.AddScoped(typeof(IServiceSysLog), typeof(ServiceSysLog));
                services.AddScoped(typeof(IServiceSysModule), typeof(ServiceSysModule));
                services.AddScoped(typeof(IServiceSysModuleAction), typeof(ServiceSysModuleAction));
                services.AddScoped(typeof(IServiceSysModuleService), typeof(ServiceSysModuleService));
                services.AddScoped(typeof(IServiceSysOAuth), typeof(ServiceSysOAuth));
                services.AddScoped(typeof(IServiceSysOrganization), typeof(ServiceSysOrganization));
                services.AddScoped(typeof(IServiceSysRole), typeof(ServiceSysRole));
                services.AddScoped(typeof(IServiceSysRoleAction), typeof(ServiceSysRoleAction));
                services.AddScoped(typeof(IServiceSysSession), typeof(ServiceSysSession));
                services.AddScoped(typeof(IServiceSysTableLog), typeof(ServiceSysTableLog));
                services.AddScoped(typeof(IServiceSysUser), typeof(ServiceSysUser));
                services.AddScoped(typeof(IServiceSysUserAddress), typeof(ServiceSysUserAddress));
                services.AddScoped(typeof(IServiceSysUserBankCard), typeof(ServiceSysUserBankCard));
                services.AddScoped(typeof(IServiceSysUserOAuth), typeof(ServiceSysUserOAuth));
                services.AddScoped(typeof(IServiceSysUserRole), typeof(ServiceSysUserRole));
                #endregion

                #region 添加微信相关
                //services.AddTransient<WX.XCX.TplMsg>();
                #endregion

                #region 添加系统相关
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                services.AddMemoryCache();//添加缓存
                services.AddControllersWithViews().AddMvcOptions(opts =>
                {
                    // 自定义模型绑定（Newtonsoft.Json）
                    //opts.ModelBinderProviders.Insert(0, new JsonModelBinderProvider());
                }).AddNewtonsoftJson();//取代之前的AddMvc
                services.AddRazorPages();
                //解决中文被编码
                services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));

                services.Configure<SiteConfig>(Configuration.GetSection("SiteConfig"));
                #endregion

                #region 添加托管等其他服务
                //services.Configure<HTConfig>(Configuration.GetSection("HTConfig"));
                //services.AddHostedService<HostedServiceHTRealtime>();
                //services.AddHostedService<HostedServiceHTRecord>();
                #endregion
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "ConfigureServices出错");
            }
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}
